package org.openkoala.koala.popup.generatecode;

import java.io.File;
import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.openkoala.koala.config.DBConfigService;
import org.openkoala.koala.config.vo.DBConfigVO;
import org.openkoala.koala.deploy.db2entity.DB2EntityUtil;

/**
 * 数据库配置界面
 * @author lingen
 *
 */
public class DatabaseConfigDialog extends Dialog {
	
	private Text driverUri;
	
	private DBConfigVO dbConfigVO;
	
	private DB2EntityUtil db2EntityUtil;
	
	private Text connectUrl;
	
	private Text jdbcDriver;
	
	private Text username;
	
	private Text password;
	
	private Shell shell;
	public DatabaseConfigDialog(Shell shell, String projectPath,
			DB2EntityUtil db2EntityUtil) {
		super(shell);
		this.shell = shell;
		this.dbConfigVO = DBConfigService.queryDBConfig(projectPath);
		this.db2EntityUtil = db2EntityUtil;
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		initializeDialogUnits(composite);

		GridData mainGridData = new GridData(480, 300);
		composite.setLayoutData(mainGridData);

		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 1;
		composite.setLayout(gridLayout);

		createDatabaseConfigComposite(composite);
		createChooseDriverComposite(composite);
		
		return composite;
	}
	
	private void createDatabaseConfigComposite(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		initializeDialogUnits(composite);

		composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		gridLayout.marginTop = 15;
		composite.setLayout(gridLayout);
		
		FontData fontData = new FontData();
	    fontData.setStyle(SWT.BOLD);
	    fontData.setHeight(13);
	    Font labelFont = new Font(null, fontData);
		
		GridData textGridData = new GridData(GridData.FILL_HORIZONTAL);
		
//		Label databaseTypeLabel = new Label(composite, SWT.NONE);
//		databaseTypeLabel.setText("Database Type:");
//		databaseTypeLabel.setFont(labelFont);
//		Text databaseType = new Text(composite, SWT.NONE);
//		databaseType.setText(dbConfigVO.getDbType());
//		databaseType.setEditable(false);
		
		Label connectUrlLabel = new Label(composite, SWT.NONE);
		connectUrlLabel.setText("Connect Url:");
		connectUrlLabel.setFont(labelFont);
		connectUrl = new Text(composite, SWT.MULTI | SWT.WRAP|SWT.BORDER);
		connectUrl.setText(dbConfigVO.getConnectionURL());
		connectUrl.setLayoutData(textGridData);
		
		Label jdbcDriverLabel = new Label(composite, SWT.NONE);
		jdbcDriverLabel.setText("JDBC Driver Class:");
		jdbcDriverLabel.setFont(labelFont);
		jdbcDriver = new Text(composite, SWT.BORDER);
		jdbcDriver.setText(dbConfigVO.getJdbcDriver());
		jdbcDriver.setLayoutData(textGridData);
		
		Label usernameLabel = new Label(composite, SWT.NONE);
		usernameLabel.setText("Username:");
		usernameLabel.setFont(labelFont);
		username = new Text(composite, SWT.BORDER);
		username.setText(dbConfigVO.getUsername());
		username.setLayoutData(textGridData);
		
		Label passwordLabel = new Label(composite, SWT.NONE);
		passwordLabel.setText("Password:");
		passwordLabel.setFont(labelFont);
		password = new Text(composite, SWT.BORDER);
		password.setText(dbConfigVO.getPassword());
		password.setLayoutData(textGridData);
	}
	
	private void createChooseDriverComposite(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		initializeDialogUnits(composite);

		composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		composite.setLayout(gridLayout);

		final FileDialog fileDialog = new FileDialog(composite.getShell(), SWT.OPEN);
		
		GridData textGridData = new GridData(GridData.FILL_HORIZONTAL);
		textGridData.heightHint = 20;
		
		driverUri = new Text(composite, SWT.BORDER);
		driverUri.setLayoutData(textGridData);
		
		Button chooseFileButton = new Button(composite, SWT.PUSH);
		chooseFileButton.setText("选择数据库驱动");
		chooseFileButton.addSelectionListener(new SelectionListener() {
			
			@Override
			public void widgetSelected(SelectionEvent e) {
				fileDialog.open();
				driverUri.setText(fileDialog.getFilterPath() + File.separatorChar + fileDialog.getFileName());
			}
			
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
			}
		});
	}
	
	@Override
	protected void okPressed() {
		if (driverUri.getText().isEmpty()) {
			MessageDialog.openError(shell,
					"ERROR", "driverUri不能为空");
			return;
		}
		if (jdbcDriver.getText().isEmpty()) {
			MessageDialog.openError(shell,
					"ERROR", "jdbcDriver不能为空");
			return;
		}
		if (connectUrl.getText().isEmpty()) {
			MessageDialog.openError(shell,
					"ERROR", "connectUrl不能为空");
			return;
		}
		if (jdbcDriver.getText().isEmpty()) {
			MessageDialog.openError(shell,
					"ERROR", "jdbcDriver不能为空");
			return;
		}
		if (username.getText().isEmpty()) {
			MessageDialog.openError(shell,
					"ERROR", "username不能为空");
			return;
		}
		db2EntityUtil.init(jdbcDriver.getText().trim(), connectUrl.getText().trim(), username.getText().trim(), password.getText().trim(), driverUri.getText().trim());
		//检测连接
		try {
			db2EntityUtil.connect();
		} catch (Exception e) {
			MessageDialog.openError(shell,
					"ERROR", "数据库连接失败，请检查你的设置！");
			return;
		}
		super.okPressed();
	}
	
}
